package com.ruoyi.app.controller.upload.controller;

import com.ruoyi.app.controller.upload.entity.VO.SimpleFileVO;
import com.ruoyi.app.controller.upload.service.IUploadService;
import com.ruoyi.common.annotation.AuthMember;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.entity.DO.FileDO;
import com.ruoyi.common.entity.DO.member.MemberDO;
import com.ruoyi.common.exception.BusinessException;
import io.swagger.annotations.*;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;
import springfox.documentation.annotations.ApiIgnore;

@RestController
@RequestMapping("/api/upload")
@ApiIgnore(value = "上传文件")
public class UploadController extends BaseController {
    @Value("${ruoyi.profile}")
    private String profile;

    @Autowired
    private IUploadService uploadService;

    @ApiOperation(value = "上传图片文件", notes = "上传图片文件")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "file", value = "文件", dataTypeClass = MultipartFile.class,required = true, paramType = "query", dataType = "file"),
            @ApiImplicitParam(name = "modelId", value = "模块ID，目前只支持1", required = true, dataTypeClass = Integer.class,paramType = "query", dataType = "int",example = "1"),
            @ApiImplicitParam(name = "token", value = "授权密钥", required = true, dataTypeClass = String.class,paramType = "header"),
    })
    @ApiResponses({
            @ApiResponse(code = 200, message = ""),
            @ApiResponse(code = 401, message = "Unauthorized！"),
            @ApiResponse(code = 500, message = "没有找到上传图片！"),
            @ApiResponse(code = 500, message = "上传图片失败！")
    })
    @PostMapping("/image/{modelId}")
    public AjaxResult upload(@ApiIgnore @AuthMember MemberDO member, @PathVariable("modelId") Integer modelId, @RequestParam("file") MultipartFile file) {
        try {
            if (!file.isEmpty()) {
                FileDO fileDO = uploadService.addImageFile(modelId, member.getId(), file);
                SimpleFileVO simpleFileVO = new SimpleFileVO();
                simpleFileVO.setPath(fileDO.getPath());
                simpleFileVO.setUrl(fileDO.getUrl());
                return AjaxResult.success(simpleFileVO);
            }
            return error("没有找到上传图片！");
        } catch (BusinessException e) {
            return error(e.getMessage());
        } catch (Exception e) {
            e.printStackTrace();
            return error("上传图片失败！");
        }
    }

    @ApiOperation(value = "上传图片文件", notes = "上传图片文件")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "file", value = "文件", dataTypeClass = MultipartFile.class,required = true, paramType = "query", dataType = "file"),
            @ApiImplicitParam(name = "modelId", value = "模块ID，目前只支持1", required = true, dataTypeClass = Integer.class,paramType = "query", dataType = "int",example = "1"),
            @ApiImplicitParam(name = "token", value = "授权密钥", required = true, dataTypeClass = String.class,paramType = "header"),
    })
    @ApiResponses({
            @ApiResponse(code = 200, message = ""),
            @ApiResponse(code = 401, message = "Unauthorized！"),
            @ApiResponse(code = 500, message = "没有找到上传图片！"),
            @ApiResponse(code = 500, message = "上传图片失败！")
    })
    @PostMapping("/image64/{modelId}")
    public AjaxResult uploadBase64(@ApiIgnore @AuthMember MemberDO member, @PathVariable("modelId") Integer modelId, @RequestParam("file") String file) {
        try {
            if (!file.isEmpty()) {
                file = file.substring(file.indexOf(",",1)+1);
                FileDO fileDO = uploadService.addImageFileBase64(modelId, member.getId(), file);
                SimpleFileVO simpleFileVO = new SimpleFileVO();
                simpleFileVO.setPath(fileDO.getPath());
                simpleFileVO.setUrl(fileDO.getUrl());
                return AjaxResult.success(simpleFileVO);
            }
            return error("没有找到上传图片！");
        } catch (BusinessException e) {
            return error(e.getMessage());
        } catch (Exception e) {
            e.printStackTrace();
            return error("上传图片失败！");
        }
    }
}
